#include "status_bar.h"
#include <iostream>
#include "assert.h"

using std::cout;
using std::endl;

void StatusBar::breakBrunch(size_t v)
{
    numberBreaks_[v + 1]++;
    iter_++;
    if ((iter_ & STATUS_PERIOD) == 0) {
        log();
    }
}

void StatusBar::log()
{
    cout << "Completed part = " << statusPart() * 100.0 << endl;
    cout << "Iterations = " << iter_ << endl;
    cout << "Time from beggining = " <<
        boost::posix_time::to_simple_string(
            boost::posix_time::second_clock::universal_time()
            - startTime_) << endl;
}

double StatusBar::statusPart()
{
    size_t r = 0;
    double res = 0;
    size_t i = NUM_VERTICES + 1;
    do {
        --i;
        numberBreaks_[i] += r;
        r = (numberBreaks_[i] >> 1);
        numberBreaks_[i] &= 1;
        if (numberBreaks_[i]) {
            res += pow2_[i];
        }
    } while (i > 0);
    assert(r == 0, "r == 0");
    return res;
}

StatusBar::StatusBar():iter_(0)
{
    double cpow2 = 1.0;
    for(size_t i = 0; i < NUM_VERTICES; ++i) {
        pow2_[i] = cpow2;
        numberBreaks_[i] = 0;
        cpow2 *= 0.5;
    }
    startTime_ = boost::posix_time::second_clock::universal_time();
}

StatusBar::~StatusBar()
{
    log();
}
